<?php
class Default_Form_ValidateUserupdate{
//Chứa những thông báo lỗi của form
	protected $_messagesError;
	
	//Mảng chứa dữ liệu sau khi kiểm tra
	protected $_arrData;
	
	public function __construct($arrParam = array(),$options = null){
		//Kiểm tra Password
			$validator 	= new Zend_Validate();
			$validator	->addValidator(new Zendvn_Validate_StringLength(6,32),true)
						->addValidator(new Zendvn_Validate_Regex('#^[a-zA-Z0-9@!\#\$%\^&\*\-\+]+$#'),true);
			
			if($arrParam['password'] != "" && !$validator->isValid($arrParam['password'])){
				$messages = $validator->getMessages();
				$this->_messagesError['password'] = 'Mật khẩu : ' . current($messages);
			}	
		//Kiểm tra Password
			$validator 	= new Zend_Validate();
			$validator	->addValidator(new Zendvn_Validate_StringLength(6,32),true)
						->addValidator(new Zendvn_Validate_Regex('#^[a-zA-Z0-9@!\#\$%\^&\*\-\+]+$#'),true)
						->addValidator(new Zendvn_Validate_ConfirmPassword($arrParam['password']),true);
			
			if($arrParam['password'] != "" && !$validator->isValid($arrParam['confirm_password'])){
				$messages = $validator->getMessages();
				$this->_messagesError['confirm_password'] = 'Nhập lại mật khẩu : ' . current($messages);
			}	
		
		//phone
		$validator 	= new Zend_Validate();
		$validator	->addValidator(new Zendvn_Validate_StringLength(10,15),true)
					->addValidator(new Zendvn_Validate_Int(), true);
		if(!$validator->isValid($arrParam['phone'])){
			$messages = $validator->getMessages();
			$this->_messagesError['phone'] = 'Điện thoại: '.current($messages);
		}	
		
		//Kiểm tra First name
		$validator 	= new Zend_Validate();
		$validator	
					->addValidator(new Zendvn_Validate_StringLength(2),true);
		
		if(!$validator->isValid($arrParam['firstname'])){
			$messages = $validator->getMessages();
			$this->_messagesError['firstname'] = 'Họ : ' . current($messages);
			$arrParam['firstname'] = '';
		}
		//Kiểm tra last name
		$validator 	= new Zend_Validate();
		$validator	
					->addValidator(new Zendvn_Validate_StringLength(2),true);
		
		if(!$validator->isValid($arrParam['lastname'])){
			$messages = $validator->getMessages();
			$this->_messagesError['lastname'] = 'Tên : ' . current($messages);
			$arrParam['lastname'] = '';
		}
		
		$this->_arrData = $arrParam;
	}
	
	public function isError(){
		//kiểm tra nếu có lỗi trả về giá trị true
		if(count($this->_messagesError) > 0){
			return true;
		}else{
			return false;
		}
	}
	
	public function getMessageError($options = null){
		if($options['upload'] == true){
			$this->_arrData['user_avatar'] = $this->uploadFile('user_avatar',null);
		}
		return $this->_messagesError;
	}
	public function getData($options = null){
		if($options['upload'] == true){
			$this->_arrData['user_avatar'] = $this->uploadFile('user_avatar',null);
		}
		return $this->_arrData;
	}
	public function uploadFile($filename = null, $options = null){
		//Duong dan den thu muc upload		
		$upload_dir = FILES_PATH . '/users';
		$upload = new Zendvn_File_Upload();
		$fileInfo = $upload->getFileInfo($filename);
		$fileName = $fileInfo[$filename]['name'];		
		if(!empty($fileName)){
			$fileName = $upload->upload($filename, $upload_dir . '/orignal',
							 array('task'=>'rename'),$filename . '_');
			$thumb = Zendvn_File_Images::create($upload_dir . '/orignal/' . $fileName);
			$thumb	->resize(100,100)	->save($upload_dir . '/images100x100/' . $fileName);			
			$thumb = Zendvn_File_Images::create($upload_dir . '/orignal/' . $fileName);
			$thumb	->resize(450,450)	->save($upload_dir . '/images450x450/' . $fileName);
						
			if($this->_arrData['action']=='edit')
			{	
				$upload->removeFile($upload_dir . '/orignal/'. $this->_arrData['current_stadium_' . $filename]);
				$upload->removeFile($upload_dir . '/images100x100/'. $this->_arrData['current_stadium_' . $filename]);
				$upload->removeFile($upload_dir . '/images450x450/'. $this->_arrData['current_stadium_' . $filename]);						
			}
		}		
		return $fileName;		
	}
}